package com.zhouchb.stream;

import java.io.*;
import java.util.concurrent.ForkJoinPool;

/**
 * @Author Zhouchb
 * @Create 2021-10-2021/10/31 :22:40
 * @Description
 */
public class BufferStreamTest {
    //缓冲流 提高读取输入的速度
    public static void main(String[] args) {
    bufferTest();
    }
    public static void bufferTest(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            File file = new File("IO/small2.png");
            File fileo = new File("IO/small3.png");
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(fileo);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] buffer = new byte[5];
            int len;
            while ((len = bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭外层流，内层流自动关闭，所以关闭缓冲流就可以了
            try {
                if (bis!=null)
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bos!=null)
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
